package com.five.matebook.utils;

import android.icu.text.SimpleDateFormat;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

public class DateAlias {

    private Map<String, DateRange> mAliasMapYear = new HashMap<>();
    private Map<String, DateRange> mAliasMapMonth = new HashMap<>();
    public DateAlias() {
        mAliasMapYear.put("今年", new DateRange(DateUtils.getNowYearBegin(), DateUtils.getNowYearEnd()));
        mAliasMapYear.put("去年", new DateRange(DateUtils.getNowYearBegin(-1), DateUtils.getNowYearEnd(-1), true));
        mAliasMapMonth.put("本月", new DateRange(DateUtils.getNowMonthBegin(), DateUtils.getNowMonthEnd()));
        mAliasMapMonth.put("上个月", new DateRange(DateUtils.getNowMonthBegin(-1), DateUtils.getNowMonthEnd(-1)));
    }

    private SimpleDateFormat formatYear = new SimpleDateFormat("yyyy", Locale.getDefault());
    private SimpleDateFormat formatMonth = new SimpleDateFormat("MM", Locale.getDefault());
    private SimpleDateFormat formatRest = new SimpleDateFormat("dd HH:mm", Locale.getDefault());
    private SimpleDateFormat formatDay = new SimpleDateFormat("dd", Locale.getDefault());

    public String format(long date) {
        return format(date, false);
    }
    public String format(long date, boolean dateOnly) {
        boolean stopNextReplace = false;
        boolean ignoreYear = false;
        // 1. 替换年
        String year = formatYear.format(date);
        for (Map.Entry<String, DateRange> entry : mAliasMapYear.entrySet()) {
            if (entry.getValue().inRange(date)) {
                year = entry.getKey();
                stopNextReplace = entry.getValue().stopNextReplace;
                break;
            }
        }
        // 2. 替换月
        String month = formatMonth.format(date);
        if (!stopNextReplace) {
            for (Map.Entry<String, DateRange> entry : mAliasMapMonth.entrySet()) {
                if (entry.getValue().inRange(date)) {
                    month = entry.getKey();
                    stopNextReplace = entry.getValue().stopNextReplace;
                    ignoreYear = true;
                    break;
                }
            }
        }

        if (ignoreYear) {
            if (dateOnly) {
                return String.format("%s-%s", month, formatDay.format(date));
            }
            return String.format("%s-%s", month, formatRest.format(date));
        } else {
            if (dateOnly) {
                return String.format("%s-%s-%s", year, month, formatDay.format(date));
            }
            return String.format("%s-%s-%s", year, month, formatRest.format(date));
        }
    }

    public List<String> getAliasList() {
        List<String> ret = new ArrayList<>();
        for (Map.Entry<String, DateRange> entry : mAliasMapYear.entrySet()) {
            ret.add(entry.getKey());
        }
        for (Map.Entry<String, DateRange> entry : mAliasMapMonth.entrySet()) {
            ret.add(entry.getKey());
        }
        return ret;
    }

    public DateRange getDateRangeByAlias(String alias) {
        for (Map.Entry<String, DateRange> entry : mAliasMapYear.entrySet()) {
            if (entry.getKey().equals(alias)) {
                return entry.getValue();
            }
        }
        for (Map.Entry<String, DateRange> entry : mAliasMapMonth.entrySet()) {
            if (entry.getKey().equals(alias)) {
                return entry.getValue();
            }
        }
        return null;
    }

    public static class DateRange {
        public long begin = 0;
        public long end = 0;
        public DateRange(long begin, long end) {
            this(begin, end, false);
        }

        boolean stopNextReplace;
        public DateRange(long begin, long end, boolean stopNextReplace) {
            this.begin = begin;
            this.end = end;
            this.stopNextReplace = stopNextReplace;
        }

        public boolean inRange(long date) {
            return date >= this.begin && date <= this.end;
        }
    }
}
